package com.singfan.app.controller;

import com.singfan.app.controller.model.HttpModelConverter;
import com.singfan.app.controller.model.request.MainSearchRequest;
import com.singfan.app.controller.model.request.ShopRecommendListRequest;
import com.singfan.app.controller.model.response.*;
import com.singfan.app.controller.model.response.partial.BarberPartial;
import com.singfan.app.controller.model.response.partial.HairstylePartial;
import com.singfan.app.controller.model.response.partial.ShopSummaryPartial;
import com.singfan.app.util.RequestParamChecker;
import com.singfan.common.util.GPSUtil;
import com.singfan.core.dao.database.model.Barber;
import com.singfan.core.dao.database.model.BarberHairstyle;
import com.singfan.core.dao.database.model.ShopHair;
import com.singfan.core.dao.database.model.constant.SearchType;
import com.singfan.core.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: hansonghe
 * Date: 2015-09-02 15:56
 * Desc:
 */
@Controller
public class MainController extends SingFanControllerBase {
    @Autowired
    private IShopService shopService;

    @RequestMapping(value="/v1/main/search", method= RequestMethod.POST)
    public void search(HttpServletRequest request, HttpServletResponse response){
        MainSearchResponse result = new MainSearchResponse();
        MainSearchRequest mainSearchRequest = request2Bean(request, MainSearchRequest.class);

        String ip = getIpAddress(request);
        String accessToken = mainSearchRequest.getAccessToken();
        int searchType = mainSearchRequest.getSearchType();
        String searchText = mainSearchRequest.getSearchText();
        int pageIndex = mainSearchRequest.getPageIndex();
        int pageSize = mainSearchRequest.getPageSize();
        String longitudeStr = mainSearchRequest.getLongitude();
        String latitudeStr = mainSearchRequest.getLatitude();

        boolean checkResult = true;
        if (!SearchType.isSearchType(searchType)){
            checkResult = false;
        }
        else if (!RequestParamChecker.checkPageIndexAndSize(pageIndex, pageSize)){
            checkResult = false;
        }
        else if (!RequestParamChecker.checkLongitude(longitudeStr)){
            checkResult = false;
        }
        else if (!RequestParamChecker.checkLatitude(latitudeStr)){
            checkResult = false;
        }

        if (checkResult){
            double longitude = Double.parseDouble(longitudeStr);
            double latitude = Double.parseDouble(latitudeStr);
            if (searchType == SearchType.SHOP){
                List<ShopHair> shopHairList = shopService.searchShop(searchText, pageIndex, pageSize);
                List<ShopSummaryPartial> shopSummaryPartialList = new ArrayList<>(shopHairList.size());
                for(ShopHair shopHair : shopHairList){
                    ShopSummaryPartial shopSummaryPartial = HttpModelConverter.shopHair2ShopSummaryPartial(shopHair);
                    shopSummaryPartial.setDistance(GPSUtil.getDistance(longitude, latitude, shopHair.getLongitude().doubleValue(), shopHair.getLatitude().doubleValue()));
                    shopSummaryPartialList.add(shopSummaryPartial);
                }

                result.setSearchShopList(shopSummaryPartialList);
                if (pageIndex == 0){
                    result.setNewList(true);
                }
            }
            else if (searchType == SearchType.BARBER){
                List<Barber> barberList = barberService.searchBarberList(searchText, pageIndex, pageSize);
                List<BarberPartial> barberPartialList = new ArrayList<>(barberList.size());
                for (Barber barber : barberList){
                    BarberPartial barberPartial = HttpModelConverter.barber2BarberPartial(barber);
                    barberPartial.setDistance(GPSUtil.getDistance(longitude, latitude, barber.getLongitude().doubleValue(), barber.getLatitude().doubleValue()));
                    barberPartialList.add(barberPartial);
                }
                result.setSearchBarberList(barberPartialList);
                if (pageIndex == 0){
                    result.setNewList(true);
                }
            }
            else if (searchType == SearchType.HAIR){
                List<BarberHairstyle> hairstyleList = barberService.searchHairstyle(searchText, pageIndex, pageSize);
                List<HairstylePartial> hairstylePartialList = new ArrayList<>(hairstyleList.size());
                for (BarberHairstyle hairstyle : hairstyleList){
                    HairstylePartial hairstylePartial = HttpModelConverter.barberHairstyle2HairstylePartial(hairstyle);
                    hairstylePartialList.add(hairstylePartial);
                }

                result.setSearchHairstyleList(hairstylePartialList);
                if (pageIndex == 0){
                    result.setNewList(true);
                }
            }
            result.setResultCode(ResultCode.SUCCESS);
            traceLog.debug(String.format("MainController.search() successful -> resultCode:%1$d, ip:%2$s, accessToken:%3$s, searchType:%4$d, searchText:%5$s, pageIndex:%6$d, pageSize:%7$d", ResultCode.SUCCESS, ip, accessToken, searchType, searchText, pageIndex, pageSize));
        }
        else {
            result.setResultCode(ResultCode.REQUEST_PARAM_ERROR);
            traceLog.error(String.format("MainController.search() error with REQUEST_PARAM_ERROR -> resultCode:%1$d, ip:%2$s, accessToken:%3$s, searchType:%4$d, searchText:%5$s, pageIndex:%6$d, pageSize:%7$d", ResultCode.REQUEST_PARAM_ERROR, ip, accessToken, searchType, searchText, pageIndex, pageSize));

        }

        bean2Response(response, result);
    }

    @RequestMapping(value="/v1/main/shoprecommendlist", method= RequestMethod.POST)
    public void shopRecommendListRequest(HttpServletRequest request, HttpServletResponse response){
        ShopRecommendListResponse result = new ShopRecommendListResponse();
        ShopRecommendListRequest shopRecommendListRequest = request2Bean(request, ShopRecommendListRequest.class);

        String ip = getIpAddress(request);
        String accessToken = shopRecommendListRequest.getAccessToken();
        int shopMasterId = shopRecommendListRequest.getShopMasterId();
        int pageIndex = shopRecommendListRequest.getPageIndex();
        int pageSize = shopRecommendListRequest.getPageSize();
        String longitudeStr = shopRecommendListRequest.getLongitude();
        String latitudeStr = shopRecommendListRequest.getLatitude();

        if (RequestParamChecker.checkPageIndexAndSize(pageIndex, pageSize)
                && RequestParamChecker.checkLongitude(longitudeStr)
                && RequestParamChecker.checkLatitude(latitudeStr)){
            double longitude = Double.parseDouble(longitudeStr);
            double latitude = Double.parseDouble(latitudeStr);
            List<ShopHair> shopHairList = shopService.findSlaveShopHairList(shopMasterId, pageIndex, pageSize, longitude, latitude);
            List<ShopSummaryPartial> shopSummaryPartialList = new ArrayList<>(shopHairList.size());
            for (ShopHair shopHair : shopHairList){
                ShopSummaryPartial shopSummaryPartial = HttpModelConverter.shopHair2ShopSummaryPartial(shopHair);
                shopSummaryPartial.setDistance(GPSUtil.getDistance(longitude, latitude, shopHair.getLongitude().doubleValue(), shopHair.getLatitude().doubleValue()));
                shopSummaryPartialList.add(shopSummaryPartial);
            }

            result.setShopList(shopSummaryPartialList);
            result.setResultCode(ResultCode.SUCCESS);
            traceLog.debug(String.format("MainController.shoprecommendlist() successful -> resultCode:%1$d, ip:%2$s, accessToken:%3$s, shopMasterId:%4$d, pageIndex:%5$d, pageSize:%6$d, longitude:%7$s, latitude::%8$s", ResultCode.REQUEST_PARAM_ERROR, ip, accessToken, shopMasterId, pageIndex, pageSize, longitudeStr, latitudeStr));
        }
        else {
            result.setResultCode(ResultCode.REQUEST_PARAM_ERROR);
            traceLog.error(String.format("MainController.shoprecommendlist() error with REQUEST_PARAM_ERROR -> resultCode:%1$d, ip:%2$s, accessToken:%3$s, shopMasterId:%4$d, pageIndex:%5$d, pageSize:%6$d, longitude:%7$s, latitude::%8$s", ResultCode.REQUEST_PARAM_ERROR, ip, accessToken, shopMasterId, pageIndex, pageSize, longitudeStr, latitudeStr));
        }

        bean2Response(response, result);
    }
}
